/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * produto_quantidade.java
 *
 * Created on 10/06/2011, 01:45:34
 */
package Visualização;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Pedro
 */
public class produto_quantidade extends javax.swing.JFrame {

    /** Creates new form produto_quantidade */
    public produto_quantidade() {
        initComponents();
        
        this.setLocationRelativeTo(null);
        preencher();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tituloLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaShow = new javax.swing.JTable();
        selecionarButton = new javax.swing.JButton();
        excluirButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        tituloLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        tituloLabel.setText("Listar produtos com estoque baixo");

        tabelaShow.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Id", "Nome", "Quantidade", "Qtde Mín"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaShow.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tabelaShowKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tabelaShow);

        selecionarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/Escolher.png"))); // NOI18N
        selecionarButton.setText("Alterar");
        selecionarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selecionarButtonActionPerformed(evt);
            }
        });

        excluirButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/Lixeira.png"))); // NOI18N
        excluirButton.setText("Excluir");
        excluirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tituloLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(selecionarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(excluirButton, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tituloLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(selecionarButton)
                    .addComponent(excluirButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void selecionarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selecionarButtonActionPerformed
        int linha, id;
        
        BO.produtoBO pBO;
        pBO = new BO.produtoBO();
        
        linha = tabelaShow.getSelectedRow();
        id = Integer.parseInt(tabelaShow.getValueAt(linha, 0).toString());
        pBO.setIdBO(id);
        
        new produto_alteracao(pBO).show();
}//GEN-LAST:event_selecionarButtonActionPerformed

    private void excluirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirButtonActionPerformed
        int linha, id, op, i;
        op = JOptionPane.showConfirmDialog(null, "Deseja excluir a linha selecionada?", "Excluir", JOptionPane.YES_NO_OPTION);
        if(op==JOptionPane.YES_OPTION) {
            String codigo;
            BO.produtoBO pBO;
            Negocio.produto p;
            
            pBO = new BO.produtoBO();
            linha = tabelaShow.getSelectedRow();
            codigo = tabelaShow.getValueAt(linha, 0).toString();
            pBO.setIdBO(Integer.parseInt(codigo));
            pBO.setNomeBO(tabelaShow.getValueAt(linha, 1).toString());
            
            p = new Negocio.produto();
            p.Excluir(pBO);
            
            preencher();
        }
    }//GEN-LAST:event_excluirButtonActionPerformed

    private void tabelaShowKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tabelaShowKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) 
        {
            selecionarButtonActionPerformed(null);
        }
    }//GEN-LAST:event_tabelaShowKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new produto_quantidade().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton excluirButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton selecionarButton;
    private javax.swing.JTable tabelaShow;
    private javax.swing.JLabel tituloLabel;
    // End of variables declaration//GEN-END:variables
    void preencher(){
        int i, tamanho, j=0;

        BO.produtoBO pBO = new BO.produtoBO();
        Negocio.produto p = new Negocio.produto();
        
        p.Minimo(pBO);
        
        String mat[][] = new String[500][4];
        tamanho = pBO.getTam();
        mat = pBO.getMat2();
        
        tabelaShow.getColumnModel().getColumn(0).setPreferredWidth(20);
        tabelaShow.getColumnModel().getColumn(1).setPreferredWidth(150);
        tabelaShow.getColumnModel().getColumn(2).setPreferredWidth(35);
        tabelaShow.getColumnModel().getColumn(3).setPreferredWidth(35);
        DefaultTableModel modelo = (DefaultTableModel)tabelaShow.getModel();
        modelo.setNumRows(0); 
        for(i=0;i<tamanho;i++)
            modelo.addRow(new Object [] {mat[i][j],mat[i][j+1],mat[i][j+2],mat[i][j+3]});
    }
}
